package com.hmall.user.web;

import com.hmall.user.pojo.User;
import com.hmall.user.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @GetMapping("{id}")
    public User queryUserById(@PathVariable("id") Long userId) {
        return userService.getById(userId);
    }

    @PostMapping("update")
    @Transactional
    public Boolean updateBalance(@RequestBody Map map) {
        Long userId = new Long(map.get("userId").toString());
        Long balance = new Long(map.get("balance").toString());
        if(balance < 0){
           throw new RuntimeException("用户余额不足~~");
        }
        User user = new User();
        user.setId(userId);
        user.setBalance(balance);
        return userService.updateById(user);
    }

}
